#include <iostream>

class House;

class Mediator
{
public:
    virtual void SendMessage(const std::string & msg, House * p) = 0;
};

class House
{
public:
    House(Mediator * m, const std::string & name){this->m = m; this->name = name;}
    virtual void SendMessage(const std::string & msg) = 0;
    virtual void GetMessage(const std::string & msg) = 0;
protected:
    Mediator *m;        //中介者
    std::string name;
};

class SellHouse : public House
{
public:
    using House::House;
    void SendMessage(const std::string & msg)
    {
        m->SendMessage(msg, this);
    }
    void GetMessage(const std::string & msg)
    {
        std::cout<<" 卖房子"<<name<<"收到消息："<<msg<<std::endl;
    }
};

class BuyHouse : public House   //买房子的人
{
public:
    using House::House;
    void SendMessage(const std::string & msg)
    {
        m->SendMessage(msg, this);
    }
    void GetMessage(const std::string & msg)
    {
        std::cout<<" 买房子"<<name<<"收到消息："<<msg<<std::endl;
    }
};

class ConcreteMediator : public Mediator   //中介者
{
public:
    void SetHouse(House * sell, House * buy)
    {
        this->sell = sell;
        this->buy = buy;
    }
    void SendMessage(const std::string &msg, House *p)
    {
        if(p == sell)               //卖房子发出消息
        {
            buy->GetMessage(msg);   //买房子收到消息
        }
        else                        //买房子发出消息
        {
            sell->GetMessage(msg);  //卖房子收到消息
        }
    }
private:
    House *sell;
    House *buy;
};

int main()
{
    ConcreteMediator *m = new ConcreteMediator;
    House *s1 = new SellHouse(m, "阿大");
    House *s2 = new SellHouse(m, "二哥");

    House *buy1 = new BuyHouse(m, "三子");
    House *buy2 = new BuyHouse(m, "老四");
    
    m->SetHouse(s1, buy1);          //建立联系
    buy1->SendMessage("一口价三百万卖不卖");
    s1->SendMessage("不卖");
    //卖房子阿大收到消息：一口价三百万卖不卖
    //买房子三子收到消息：不卖
    return 0;

}